﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using System.Drawing;
using ZYCommon;
using System.Drawing.Imaging;
using System.Drawing.Drawing2D;
using System.IO;
using Bifrost;
using System.Threading;
using System.Windows.Forms;

namespace TextEditor.TextDocument.Document
{
    public sealed class UpLoadImg
    {
        UcEditorControl document = null;
        static UpLoadImg _instance = null;
        static readonly object syncRoot = typeof(object);
        public UpLoadImg()
        {
            string url = string.Empty;
            //try
            //{
            //    //XmlDocument xmlD = new XmlDocument();
            //    //xmlD.Load(Application.StartupPath + @"\tools.xml"); 
            //    //url = xmlD.SelectSingleNode("*/emrurl[@name='url']").InnerText.Trim();
            //}
            //catch
            //{
            //    MessageBox.Show("配置文件错误!");
            //}
            //if (!string.IsNullOrEmpty(url))
            //{
            //url1 = "http://192.168.1.116/Uploader.ashx";
            //sercer.Url = "http://192.168.1.116/WebService.asmx";
            //}
        }

        public static UpLoadImg Instance
        {
            get 
            {
                if (_instance == null)
                {
                    lock (syncRoot)
                    {
                        if (_instance == null)
                        {
                            _instance = new UpLoadImg();
                        }
                    }
                }
                return _instance;
            }
        }

        string url1 =string.Empty;
      //  localhost.WebService sercer = new localhost.WebService();
        System.Net.WebClient myWebClient = new System.Net.WebClient();
        public void LoadingImg(string dictStr)
        {
            //if (string.IsNullOrEmpty(url1))
            //    return;
            //string[] files = Directory.GetFiles(App.SysPath + "//temp");//获取本地图片路径
            //sercer.DeleteDict(dictStr);
            //foreach (string fileName in files) 
            //{
            //    myWebClient.UploadFile(url1, "POST", fileName);
            //    File.Delete(fileName);
            //}
        }


        public void SaveImags(Dictionary<string, string> pageheader, UserInfo us, XmlDocument xmlDoc)
        {
            if (document == null)
            {
                document = new UcEditorControl();
                document.EMRDoc.Locked = true;
            }
            document.EMRDoc.PageHeader = null;
            document.EMRDoc.PageHeader = pageheader;
            document.EMRDoc.Us = us;
            document.EMRDoc.FromXML(xmlDoc.DocumentElement, true);
            string[] strs;
            Image[] imgs = document.EMRDoc.GetDocForImages(out strs);
            string[] base64str = new string[imgs.Length];
            string fileName = string.Format(App.SysPath + "\\temp\\{0}_{1}", us.InpatientInfo.Id.ToString(), us.Tid.ToString());

            for (int i = 0; i < imgs.Length; i++)
            {
                GetPicThumbnail(imgs[i], fileName + "_" + (i + 1).ToString() + ".jpg", 100);
            }
            LoadingImg("/" + us.InpatientInfo.Id.ToString() + "/" + us.Tid.ToString());
        }

        /// <summary>
        /// 无损压缩图片
        /// </summary>
        /// <param name="sFile">原图片</param>
        /// <param name="dFile">压缩后保存位置</param>
        /// <param name="dHeight">高度</param>
        /// <param name="dWidth"></param>
        /// <param name="flag">压缩质量 1-100</param>
        /// <returns></returns>
        public static bool GetPicThumbnail(Image sFile, string dFile, int flag)
        {

            System.Drawing.Image iSource = sFile;
            ImageFormat tFormat = iSource.RawFormat;
            Bitmap ob = new Bitmap(sFile);
            //以下代码为保存图片时，设置压缩质量
            EncoderParameters ep = new EncoderParameters();
            long[] qy = new long[1];
            qy[0] = flag;//设置压缩的比例1-100
            EncoderParameter eParam = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, qy);
            ep.Param[0] = eParam;
            try
            {
                ImageCodecInfo[] arrayICI = ImageCodecInfo.GetImageEncoders();
                ImageCodecInfo jpegICIinfo = null;
                for (int x = 0; x < arrayICI.Length; x++)
                {
                    if (arrayICI[x].FormatDescription.Equals("JPEG"))
                    {
                        jpegICIinfo = arrayICI[x];
                        break;
                    }
                }
                if (jpegICIinfo != null)
                {
                    ob.Save(dFile, jpegICIinfo, ep);//dFile是压缩后的新路径
                }
                else
                {
                    ob.Save(dFile, tFormat);
                }
                return true;
            }
            catch
            {
                return false;
            }
            finally
            {
                iSource.Dispose();
                ob.Dispose();
            }
        }
    }
}
